package com.zhangdi.dwperms;

import com.zhangdi.dwperms.enums.LoginType;
import com.zhangdi.dwperms.realm.JwtRealm;
import com.zhangdi.dwperms.realm.NormalRealm;
import com.zhangdi.dwperms.token.MSToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.Collection;

public class CustomModularRealmAuthenticator extends ModularRealmAuthenticator {
    @Override
    protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {
        MSToken msToken = (MSToken) token;
        //根据身份信息中的认证方式返回一个Realm
        for (Realm realm : realms) {
            if (realm instanceof NormalRealm && msToken.getLoginType() == LoginType.account) {
                return super.doSingleRealmAuthentication(realm, token);
            } else if (realm instanceof JwtRealm && msToken.getLoginType() != LoginType.account) {
                return super.doSingleRealmAuthentication(realm, token);
            }
        }
        return null;
    }
}
